package com.flyfox.modules.hr.form;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.LinkedHashMap;

import com.flyfox.modules.hr.model.Salary;
import com.flyfox.util.DateUtils;
import com.flyfox.util.StrUtils;
import com.flyfox.util.encrypt.DESUtils;

/**
 * 薪酬导入，导出唯一键解决 TODO 如果真想用，需要完成Map定期清理现场
 * 
 * 2014年10月16日 下午5:22:04 flyfox 330627517@qq.com
 */
public class SalaryUtils {

	private static final String DES_KEY = "aflyfoxa";
	private static final String separator = DES_KEY;
	private static final String PRE = "F";
	private static final SecureRandom random = new SecureRandom();
	private static final DESUtils des = new DESUtils(DES_KEY);
	private static final LinkedHashMap<String, String> keyMap = new LinkedHashMap<String, String>();

	public static String genCode(Salary salary) {
		int tmp = random.nextInt(9999);
		String value = tmp + separator + salary.getInt("per_id")//
				+ separator + salary.getInt("id") + separator + salary.getStr("p_name");
		value = des.encryptString(value);
		String key = PRE + DateUtils.getNow("MMddHHmmss") + tmp;
		keyMap.put(key, value);
		// 避免被execl数字处理
		return key;
	}

	/**
	 * 返回数组信息
	 * 
	 * @data 1 personal id 2 salary id 3 personal name
	 * 
	 *       2014年10月16日 下午5:31:08 flyfox 330627517@qq.com
	 * @param key
	 * @return
	 */
	public static String[] getCode(String key) {
		if (!key.startsWith(PRE)) {
			return null;
		}
		String value = keyMap.get(key);
		if (StrUtils.isEmpty(value)) {
			return null;
		}
		String[] codes = des.decryptString(value).split(separator);
		return codes;
	}

	public static void main(String[] args) {
		Salary salary = new Salary();
		salary.put("per_id", 1000);
		salary.put("id", null);
		salary.put("p_name", "张三");
		String code = genCode(salary);
		System.out.println(code);
		String[] codes = getCode(code);
		System.out.println(Arrays.toString(codes));
	}
}
